package com.eleadmin.store.controller;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.*;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.store.entity.Warehouse;
import com.eleadmin.store.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.formula.functions.T;


/**
 *
 * 仓库管理
 * @author : Doman
 * @date : 2024-02-12 14:23:33
 * @version ：1.0
 */

@Api(tags = "仓库管理")
@RestController
@RequestMapping("/api/store/warehouse")
public class WarehouseController extends BaseController {

    @Autowired
    private WarehouseService warehouseService;

    @PreAuthorize("hasAuthority('store:warehouse:list')")
    //@ApiOperation("分页查询仓库管理")
    @GetMapping("/page")
    public JsonResult page(PageResult2<T> page, Warehouse warehouse) throws Exception{
        return warehouseService.list(page, warehouse, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:warehouse:list')")
    //@ApiOperation("查询全部仓库管理")
    @GetMapping()
    public JsonResult list(Warehouse warehouse){
        return warehouseService.list(warehouse, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:warehouse:list')")
    @ApiOperation("返回仓库select列表")
    @GetMapping("listSel")
    public JsonResult listSel(Warehouse warehouse){
        return warehouseService.listSel(warehouse, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:warehouse:list')")
    //@ApiOperation("根据id查询仓库管理")
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") String id) {
        return warehouseService.getById(id);
    }


    @PreAuthorize("hasAuthority('store:warehouse:save')")
    @OperationLog
    @ApiOperation(value ="添加仓库管理")
    @PostMapping()
    public JsonResult save(@RequestBody Warehouse warehouse) {
        try {
            return warehouseService.save(warehouse, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }

    }

    @PreAuthorize("hasAuthority('store:warehouse:update')")
    @OperationLog
    @ApiOperation(value ="修改仓库管理")
    @PutMapping()
    public JsonResult update(@RequestBody Warehouse warehouse) {
        try {
            return warehouseService.update(warehouse, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('store:warehouse:remove')")
    @OperationLog
    @ApiOperation(value ="删除仓库管理")
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") String id) {
        try {
            return warehouseService.remove(id, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

}
